package com.holyshit.exam.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;

import com.holyshit.exam.R;


/**
 * AlertDialogUtils
 * <p>
 * Created by HERO on 2017/7/11.
 */

public class AlertDialogUtils {

    /**
     * @param activity                    context
     * @param title                       title
     * @param msg                         msg
     * @param positiveButtonClickListener listener positiveButton
     */
    public static AlertDialog create(Activity activity, String title, String msg, ButtonClickListener positiveButtonClickListener) {
        return new Builder(activity, title, msg)
                .setPositiveListener(positiveButtonClickListener)
                .build();
    }

    /**
     * @param activity activity
     * @return new Builder
     */
    public static Builder newBuilder(Activity activity) {
        return new Builder(activity);
    }

    /**
     * @param activity   activity
     * @param title      title
     * @param contentMsg content
     * @return new Builder
     */
    public static Builder newBuilder(Activity activity, String title, String contentMsg) {
        return new Builder(activity, title, contentMsg);
    }

    public interface ButtonClickListener {
        void onCLick();
    }

    public static final class Builder {
        private String title;
        private String contentMsg;
        private String negativeName = "取消";
        private String positiveName = "确认";
        private boolean showOnlyButton = false;
        private int dialogStyle = R.style.AlertDialogCustom;
        private boolean cancelable = true;
        private Activity activity;
        private ButtonClickListener negativeListener, positiveListener;
        private View contentView;

        private Builder(Activity activity) {
            this.activity = activity;
        }

        private Builder(Activity activity, String title, String contextMsg) {
            this.activity = activity;
            this.title = title;
            this.contentMsg = contextMsg;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return  this;
        }

        public Builder setContentMsg(String contextMsg) {
            this.contentMsg = contextMsg;
            return this;
        }

        public Builder setView(View contentView) {
            this.contentView = contentView;
            return this;
        }

        /**
         * 取消按钮
         *
         * @param negativeListener listener
         */
        public Builder setNegativeListener(ButtonClickListener negativeListener) {
            this.negativeListener = negativeListener;
            return this;
        }

        /**
         * 确认按钮
         *
         * @param positiveListener listener
         */
        public Builder setPositiveListener(ButtonClickListener positiveListener) {
            this.positiveListener = positiveListener;
            return this;
        }

        /**
         * 取消按钮文本 默认为取消
         *
         * @param negativeName 取消按钮文本
         */
        public Builder setNegativeName(String negativeName) {
            this.negativeName = negativeName;
            return this;
        }


        /**
         * 确认按钮文本 默认为确认
         *
         * @param positiveName 确认按钮文本
         */
        public Builder setPositiveName(String positiveName) {
            this.positiveName = positiveName;
            return this;
        }

        /**
         * 是否是只显示一个确认按钮
         *
         * @param showOnlyButton boolean
         *                       <p>
         *                       默认为false
         */
        public Builder setShowOnlyButton(boolean showOnlyButton) {
            this.showOnlyButton = showOnlyButton;
            return this;
        }

        public Builder setDialogStyle(int dialogStyle) {
            this.dialogStyle = dialogStyle;
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }

        public AlertDialog build() {

            AlertDialog.Builder builder = new AlertDialog.Builder(activity, dialogStyle)
                    .setTitle(title)
                    .setMessage(contentMsg)
                    .setCancelable(cancelable)
                    .setView(contentView)
                    .setPositiveButton(positiveName, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                            if (positiveListener != null)
                                positiveListener.onCLick();
                        }
                    });
            if (!showOnlyButton) {
                builder.setNegativeButton(negativeName, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if (negativeListener != null)
                            negativeListener.onCLick();
                    }
                });
            }


            return builder.create();
        }
    }


}
